package com.jiyici.utils;
public class test_sortorder {

    /**
     * @param args
     */
    public static void main(String[] args) {
        new test_sortorder().Test();
    }

    private static void _ASSERTE(boolean b) {
        if (!b) {
            System.err.println("assert failed");
            Thread.dumpStack();
            System.exit(-1);
        }
    }

    int statsOpPrepend = 0;
    int statsOpAppend = 0;
    int statsOpDelete = 0;
    int statsOpInsert = 0;
    int statsOpInsertAt7 = 0;

    void TestBefore(String original, String expected) {
        String before = SortOrder.Before(original);
        _ASSERTE(before.equals(expected));
        _ASSERTE(before.compareTo(original) < 0);
    }

    void TestAfter(String original, String expected) {
        String after = SortOrder.After(original);
        _ASSERTE(after.equals(expected));
        _ASSERTE(after.compareTo(original) > 0);
    }

    void TestBetween(String a, String b) {
        String between = SortOrder.Between(a, b);
        // _ASSERTE(between == expected);
        _ASSERTE(between.compareTo(a) > 0);
        _ASSERTE(between.compareTo(b) < 0);
    }
    
    int testCount = 0;
    
    void myTestBetween(String a, String b) {
        if (testCount > 1000)
            return;
        String between = SortOrder.Between(a, b);
        System.out.println("a = " + a + ", b = " + b + ", between = " + between);
        _ASSERTE(between.compareTo(a) > 0);
        _ASSERTE(between.compareTo(b) < 0);
        testCount++;
        //System.out.println("test count: " + testCount);
        //System.out.println("test count left");
        myTestBetween(a, between);
        //System.out.println("test count right");
        //myTestBetween(between, b);
    }
    
    void TestInit() {
        String init = SortOrder.Initial();
        String before = SortOrder.Before(init);
        String after = SortOrder.After(init);
        _ASSERTE(before.compareTo(init) < 0);
        _ASSERTE(after.compareTo(init) > 0);
        TestBetween(before, init);
        TestBetween(init, after);
    }

    void Test() {
        TestInit();
        
        TestBefore("abc", "abb");
        TestBefore("abb", "aaz");
        TestBefore("aab", "aaazz");

        TestAfter("abc", "abd");
        TestAfter("abz", "acb");
        TestAfter("abzzz", "acaab");
        TestAfter("zzz", "zzzab");

        TestBetween("b", "d");
        TestBetween("b", "c");
        TestBetween("x", "xb");
        TestBetween("x", "xab");
        TestBetween("xz", "y");
        
        myTestBetween("xz", "y");
    }

    void CheckLT(int i, String a, String b) {
        if (a.compareTo(b) < 0)
            return;

        System.out.println("@" + i + ": '" + a + "' < '" + b + "' FAILS");
    }
}
